<?php
/**
 * Created by PhpStorm.
 * User: shenyvlin
 * Date: 2019/3/3
 * Time: 11:13
 */
namespace app\api\model;

use think\Model;
use think\db;
class Paymodel extends Model{
    /**
     * 查询待支付现货出价订单
     */
    public function getSpotOrderInfo($order_id){
        $where = [
            'order_id' => $order_id,
            'order_status' => 1
        ];
        $info = Db::name('spot_order_info')->where($where)->find();
        if($info){
            return $info;
        }else{
            return false;
        }
    }

    /**
     * 查询待支付订单
     */
    public function getOrderInfo($order_id){
        $where = [
            'order_id' => $order_id,
            'order_status' => 1
        ];
        $info = Db::name('order_info')->where($where)->find();
        if($info){
            return $info;
        }else{
            return false;
        }
    }

    public function getOrderDetail($order_id){
        $info = Db::name('lj_order')
            ->where('order_id',$order_id)
            ->find();
        if($info){
            return $info;
        }else{
            return false;
        }
    }

    public function updateOrder($data, $where){
        $res = db('lj_order')->where($where)->update($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 操作用户认证
     */
    public function authUser($order_id){
        $info = Db::name('lj_order')->where('order_id',$order_id)->find();
        $uid = $info['uid'];
        $data = ['auth'=>1];
        $res = db('lj_users')->where('id',$uid)->update($data);
        if($res){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 根据订单类型处理后续事务
     */
    public  function takeOrderOf($order_id){
        $info = Db::name('lj_order')->field('uid,product_id,order_type')->where('order_id',$order_id)->find();
        switch ($info['order_type']){
            case 1: // 认证费
                db('lj_users')->where('id',$info['uid'])->update(['auth'=>1]);
                break;
            case 2: // 开通vip
                $product_info = db('lj_product')->field('product_time')->where('id',$info['product_id'])->find();
                $user_info = db('lj_users')->field('vip_time')->where('id',$info['uid'])->find();
                if($product_info && $user_info){
                    $now = date('Y-m-d H:i:s',time());
                    if($user_info['vip_time']){//之前开通过
                        if($user_info['vip_time'] > $now){ //未到期
                            $old_time = strtotime($user_info['vip_time']);
                        }else{
                            $old_time = strtotime($now);
                        }
                    }else{
                        $old_time = strtotime($now);
                    }
                    $end_time = date('Y-m-d H:i:s', strtotime ("+".$product_info['product_time']." month", $old_time));
                    db('lj_users')->where('id',$info['uid'])->update(['vip'=>1,'vip_time'=>$end_time]);
                }
                break;
            case 3: // 购买金币
                $product_info = db('lj_product')->field('product_describe')->where('id',$info['product_id'])->find();
                $user_info = db('lj_users')->field('gold')->where('id',$info['uid'])->find();
                $gold_nums = $user_info['gold'] + (int)$product_info['product_describe'];
                $user_gold = db('lj_users')->where('id',$info['uid'])->update(['gold'=>$gold_nums]);
                if($user_gold){
                    $gold_data = [
                        'uid' => $info['uid'],
                        'type' => 1,
                        'num' => (int)$product_info['product_describe'],
                        'mode' => 'buy',
                        'mode_about' => $order_id,
                        'remark' => '支付购买'.$product_info['product_describe'].'金币',
                        'create_time' => date('Y-m-d H:i:s',time())
                    ];
                    db('lj_gold_record')->insert($gold_data);
                }
                break;
            case 4:

                break;
            case 5: // 报名活动
                $sign = db('lj_activity_sign')->where('order_id',$order_id)->update(['status'=>1]);
                if($sign){
                    $ac_info = db('lj_users_baseinfo')->alias('b')->field('b.sex,s.activity_id')
                        ->join('lj_activity_sign s','b.uid=s.uid')
                        ->where('b.uid',$info['uid'])->find();
                    if($ac_info['sex'] == 1){
                        db('lj_activity')->where('id',$ac_info['activity_id'])->setInc('sign_man');
                    }else{
                        db('lj_activity')->where('id',$ac_info['activity_id'])->setInc('sign_women');
                    }
                }
                break;
        }

    }
}